<?php

namespace app\admin\controller\product;

use app\common\controller\Backend;

// 商品单位管理
class Unit extends Backend
{
    protected $model = null;

    protected $searchFields = ['id','name'];

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Product.Unit');
    }

    // 商品单位列表
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax())
        {
            if ($this->request->request('keyField')) 
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        return $this->view->fetch();
    }

    // 新增商品单位
    public function add()
    {
        if($this->request->isPost())
        {
            $params = $this->request->param('row/a');

            $data = [
                'name' => $params['name']
            ];

            $result = $this->model->validate('common/Product/Unit')->save($data);

            if($result === FALSE)
            {
                $this->error($this->model->getError());
                exit;
            }else
            {
                $this->success();
                exit;
            }
        }

        return $this->view->fetch();
    }

    // 编辑商品单位
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if(!$row)
        {
            $this->error(__('该商品单位不存在，请重新选择'));
            exit;
        }

        if($this->request->isPost())
        {
            $params = $this->request->param('row/a');

            $data = [
                'id' => $ids,
                'name' => $params['name']
            ];

            $result = $this->model->validate('common/Product/Unit')->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->model->getError());
                exit;
            }else
            {
                $this->success();
                exit;
            }
        }

        $this->assign('row', $row);
        return $this->view->fetch();
    }

    // 删除商品单位
    public function del($ids = null)
    {
        $list = $this->model->select($ids);

        if(empty($list))
        {
            $this->error(__('No Results were found'));
            exit;
        }

        $result = $this->model->destroy($ids);

        if($result === FALSE)
        {
            $this->error($this->model->getError());
            exit;
        }else
        {
            $this->success();
            exit;
        }
    }
}